package cn.yhjz.platform.system.controller;

import cn.yhjz.common.AjaxResponse;
import cn.yhjz.platform.system.entity.LogedUserDetails;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.service.ISysUserService;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.platform.system.vo.LogedUser;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 登录相关的
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 页面初始化时获得用户的所有信息
     *
     * @return
     */
    @RequestMapping("/getCurrentUser")
    public AjaxResponse getCurrentUser() {
        SysUser currentUser = LoginUtil.getCurrentUser();
        SecurityContext context = SecurityContextHolder.getContext();
        if (context != null) {
            Authentication authentication = context.getAuthentication();
            if (authentication != null) {
                Object obj = authentication.getPrincipal();
                if (obj != null && obj instanceof LogedUserDetails) {
                    LogedUserDetails user = (LogedUserDetails) authentication.getPrincipal();
                    //查询数据库，得到用户的相关信息
                    SysUser queryParam = new SysUser();
                    queryParam.setUsername(user.getUsername());
                    //把spring security的用户对象转换成前端需要的对象
                    LogedUser logedUser = LogedUser.fromSecurityUser(currentUser, user);
                    logedUser.setMenuTree(SysMenuController.buildTree(sysUserService.getUserMenuList(currentUser)));
                    return AjaxResponse.success(logedUser);
                }
            }
        }
        return AjaxResponse.fail("未登录");
    }

    /**
     * 修改密码
     *
     * @param oldPassword  旧密码
     * @param newPassword1 新密码
     * @param newPassword2 重复新密码
     * @return
     */
    @PostMapping("/changePassword")
    public AjaxResponse changePassword(HttpSession session, String oldPassword, String newPassword1, String newPassword2) {
        SysUser currentUser = LoginUtil.getCurrentUser();
        if (StrUtil.isBlank(newPassword1) || StrUtil.isBlank(newPassword2)) {
            return AjaxResponse.fail("参数错误");
        }
        if (!StrUtil.equals(newPassword1, newPassword2)) {
            return AjaxResponse.fail("两个新密码不一样");
        }
        SysUser nowUser = sysUserService.selectById(currentUser.getId());
        if (!passwordEncoder.matches(oldPassword, nowUser.getPassword())) {
            return AjaxResponse.fail("原密码错误");
        }
        nowUser.setPassword(passwordEncoder.encode(newPassword1));
        sysUserService.updateById(nowUser);
        return AjaxResponse.success();
    }
}
